/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package session

import (
	"net/http"
	"gitee.com/tugoer/uuid"
	"time"
)

func HttpCookeSessionID(w http.ResponseWriter, r *http.Request) string {
	var id string
	cookie, _ := r.Cookie(Env.CookieKey)
	if cookie != nil {
		id = cookie.Value
	} else {
		id = uuid.New()
	}

	// update cookie
	http.SetCookie(w, &http.Cookie{
		Name:     Env.CookieKey,
		Value:    id,
		Path:     "/",
		Expires:  time.Now().Add(time.Duration(Env.ExpireTime)),
	})
	return id
}

func HttpSession(writer http.ResponseWriter, request *http.Request) *Session {
	return DefaultContainer.GetSession(HttpCookeSessionID(writer, request))
}